package com.ykdl.member.views;

import net.wxxr.dataparse.model.DataParseError;
import net.wxxr.dataparse.model.DataState;
import net.wxxr.http.interfaces.ITag;
import net.wxxr.http.request.HttpBaseRequest;
import oauth.signpost.http.HttpParameters;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.LinearLayout;

import com.ykdl.member.kid.R;
import com.ykdl.member.KidApp;
import com.ykdl.member.constant.KidAction;
import com.ykdl.member.constant.KidConfig;
import com.ykdl.member.kid.beans.HomeMessageCount;
import com.ykdl.member.kid.chat.ChatActivity;
import com.ykdl.member.kid.circle.AllCircleActivity;
import com.ykdl.member.kid.circle.MyCircleActivity;
import com.ykdl.member.kid.doctor.ToAskDoctorActivity;
import com.ykdl.member.kid.gears.ArticlesWebviewActivity;
import com.ykdl.member.kid.gears.FindOtherActivity;
import com.ykdl.member.kid.gears.GoodLifeActivity;
import com.ykdl.member.kid.gears.HomeActivity;
import com.ykdl.member.kid.gears.MyGoodLifeActivity;
import com.ykdl.member.kid.marketcard.IntegralDetailsActivity;
import com.ykdl.member.kid.messagebox.MyMessageAcivity1;
import com.ykdl.member.kid.prefs.MyPrefs;
import com.ykdl.member.kid.util.AccessTokenKeeper;
import com.ykdl.member.kid.util.RequestAsAccessTokenUtil;
import com.ykdl.net.manager.TaskManager;
import com.ykdl.net.manager.Wxxr;
/**
 * 
 * 底部的模块切换工具类
 * @since 1.0
 */
public class IaskMenuBarView extends LinearLayout implements OnClickListener {

	Context context;
	private ImageButton bar_homepage;
	private ImageButton bar_mycircle;
	private ImageButton bar_news;
	private ImageButton bar_askdoctor;
	private Activity mBaseScreen;
	private ImageButton menu_persfind;
	private KidApp mApplication ;
	public IaskMenuBarView(Context context) {
		super(context);
		this.context = context;
		init();
	}
	
	public IaskMenuBarView(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.context = context;
		init();
	}
	
	

	private void init() {
		View view = ((LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(
				R.layout.iask_bot_menubar, null);
		addView(view, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT, 1));
		/**首页*/
		bar_homepage = (ImageButton) findViewById(R.id.bar_homepage);
		bar_homepage.setOnClickListener(this);
		
		/**圈子*/
		bar_mycircle = (ImageButton) findViewById(R.id.bar_mycircle);
		bar_mycircle.setOnClickListener(this);
		/**问医生*/
		bar_askdoctor  = (ImageButton) findViewById(R.id.bar_askdoctor);
		bar_askdoctor.setOnClickListener(this);
		
		/**消息*/
		bar_news = (ImageButton) findViewById(R.id.bar_personnews);
		bar_news.setOnClickListener(this);
		
		/**发现*/
		menu_persfind = (ImageButton) findViewById(R.id.menu_persfind);
		menu_persfind.setOnClickListener(this);
		initMessageCount();
	}
	
	
	public void setMessageBar(boolean isShow){
		if(isShow){
			bar_news.setBackgroundResource(R.drawable.men_personnews_havenew);
		}else{
			bar_news.setBackgroundResource(R.drawable.men_personnews_nowhas);
		}
			
	}
	public void setMessageNoBar(){
			bar_news.setBackgroundResource(R.drawable.men_personnews);
			
	}
	public void setMessageBar(){
		bar_news.setBackgroundResource(R.drawable.bar_personnews);
		
}
	

	public void setBaseScreen(Activity pp){
		mBaseScreen=pp;
		mApplication=(KidApp) mBaseScreen.getApplication();
	}

	/**
	 * 产品临时加的需求，获取他们已经忘了，先返回false了
	 * @return
	 */
	private boolean hasnewAction(){
//		IAskActiviQuestCacheDAO dao = new IAskActiviQuestCacheDAO(context);	
//		// allquest = dao.fetchAllData();
//		int newans = dao.fetchUnreadDataByTypeSize(IAskMyStateBean.ATT_ANSWER);
//		int newask = dao.fetchUnreadDataByTypeSize(IAskMyStateBean.ATT_ASK);
//		dao.close();
	//	return newans+newask>0;
		return false;
	}
	
	@Override
	public void onClick(View v) {
		int id = v.getId();
		switch (id) {
		case R.id.bar_homepage:
			if (!bar_homepage.isSelected()) {
					setSelectedID(R.id.bar_homepage);
					Intent t=new Intent(context,HomeActivity.class);
					context.startActivity(t);
					if(context instanceof HomeActivity)return;
					((Activity)context).finish();
			}
			break;
		case R.id.bar_mycircle:
			if (!bar_mycircle.isSelected()) {
				setSelectedID(R.id.bar_mycircle);
				Intent t=null;
				if (mApplication.mMyUser.iAnonymousToken()) {
					t = new Intent(context,AllCircleActivity.class);
					t.putExtra(KidAction.FROM, "111");
				}else {
					t = new Intent(context,MyCircleActivity.class);
				}
					context.startActivity(t);
					if(context instanceof HomeActivity)return;
					((Activity)context).finish();
				
			}
			break;
		
			
		case R.id.bar_personnews:
			if (!bar_news.isSelected()) {
				Intent t=null;
//				if (mApplication.mMyUser.iAnonymousToken()) {
//					 t=new Intent(context,RegActivity.class);
//					 t.putExtra(KidAction.FROM, "");
//					 context.startActivity(t);
//				}else {
//					setSelectedID(R.id.bar_personnews);
//					 t=new Intent(context,GoodLifeActivity.class);
//					 context.startActivity(t);
//					 if(context instanceof HomeActivity)return;
//					((Activity)context).finish();
//				}
				 t=new Intent(context,MyMessageAcivity1.class);
				 context.startActivity(t);
				 if(context instanceof HomeActivity)return;
				 ((Activity)context).finish();
			
		}
		
			break; 
		case R.id.bar_askdoctor:
			if (!bar_askdoctor.isSelected()) {
				Intent t=null;
//				if (mApplication.mMyUser.iAnonymousToken()) {
//					t=new Intent(context,RegActivity.class);
//					 t.putExtra(KidAction.FROM, "");
//					 context.startActivity(t);
//				}else {
//					setSelectedID(R.id.bar_askdoctor);
//					 t=new Intent(context,ToAskDoctorActivity.class);
//					context.startActivity(t);
//					if(context instanceof HomeActivity)return;
//				}
				t=new Intent(context,MyGoodLifeActivity.class);
				 context.startActivity(t);
				 if(context instanceof HomeActivity)return;
				 ((Activity)context).finish();
				
			}
			break;
		case R.id.menu_persfind:
			if (!menu_persfind.isSelected()) {
				setSelectedID(R.id.menu_persfind);
				Intent t=new Intent(context,FindOtherActivity.class);
				context.startActivity(t);
				if(context instanceof HomeActivity)return;
				((Activity)context).finish();
			}
			break;

		default:
			break;
		}
		

	}
	/**
	 * 获取信息数量汇总
	 */
	private void initMessageCount() {
		String url = KidConfig.UNREAD_NEWS_COUNT ;
		HttpParameters bodyParams = new HttpParameters();
		HttpBaseRequest request = Wxxr.getInstance().postBaseRequest(url, bodyParams,null
				);
		TaskManager.startHttpRequest(request, new getMessageCountTag(),
				HomeMessageCount.class);
	}
	class getMessageCountTag implements ITag{
		@Override
		public void refreshUI(Object t, DataState ds) {
			if(null != t && t instanceof HomeMessageCount){
				HomeMessageCount messageCountBean = (HomeMessageCount)t;
				if (messageCountBean.getUnread_count() > 0) {
					MyPrefs.setMessageCount(context, "1");
				}else {
					MyPrefs.setMessageCount(context, null);
				}
			}
		}

		@Override
		public boolean refreshError(DataParseError parseError) {
			return true;
		}

		@Override
		public void refreshCancel() {
		}
		
	}
	
	
	public void stnews(int rsid) {
		// TODO Auto-generated method stub
		bar_news.setBackgroundResource(rsid);
	}
	public  void setSelectedID(int id) {
		String unknownInfo=MyPrefs.getMessageCount(context);  //从本地文件中取
		if(!TextUtils.isEmpty(unknownInfo) && unknownInfo.equals("1")){
			if (id == R.id.bar_mycircle||id == R.id.menu_persfind||id == R.id.bar_askdoctor) {
				setMessageBar(true);
			}
		}
		 bar_homepage.setSelected(false);
		 bar_mycircle.setSelected(false);
		 bar_news.setSelected(false);
		 menu_persfind.setSelected(false);
		 bar_askdoctor.setSelected(false);
			if (id == R.id.bar_homepage) {
				bar_homepage.setSelected(true);
			} else if (id == R.id.bar_mycircle) {
				bar_mycircle.setSelected(true);
			}else if (id == R.id.menu_persfind) {
				menu_persfind.setSelected(true);
			}else if (id == R.id.bar_personnews) {
				bar_news.setSelected(true);
			}else if (id == R.id.bar_askdoctor) {
				bar_askdoctor.setSelected(true);
			}
		
		
	}
	

}
